<?php
/**
 * Vstupny bod celej aplikacie
 * 
 * @author Tomas Srna
 * @package bakalarka
 */

require('../bootstrap.php');

try
{
	// Vytvorime instanciu Db, aby sa pri 
	// nespravnom spojeni hodila vynimka v spravnom kontexte
	$db = Db::instance();
	PatternRouter::addPattern('/(admin)\/([^\/]*)/', "$1-$2$3");
	PatternRouter::addPattern(
		'/(domain)(\/?)([^\/]*)(\/?)([^\/]*)(\/?)([^\/]*)(\/?)/', "$1-$5/$7/$3");
	PatternRouter::addPattern(
		'/(hosting)(\/?)([^\/]*)(\/?)([^\/]*)(\/?)([^\/]*)(\/?)/', "$1-$5/$7/$3");
	PatternRouter::route(getenv('REQUEST_URI'));
}
catch(PDOException $e)
{
	$text = "L::errorDatabase_s;".json_encode(array($e->getMessage()));
	if(Db::hasInstance())
		Logger::log($text, 'error');
	$template = Template::instance();
	$template->title = L::selected()->errorDatabase;
	// Nezobrazme heslo v Stack Trace !
	$stackTrace = $e->getTraceAsString();
	$stackTrace = str_replace(Config::$dbPassword, 
		str_repeat("*", 10), $stackTrace);
	$template->text = L::translate($text).
		(DEBUG?' <pre>'."Stack Trace:\n".$stackTrace.'</pre>':'');
	$template->render('error');
}
catch(PermissionDeniedException $e)
{
	Logger::log($e->getMessage(), 'warning');
	$template = Template::instance();
	$template->title = L::selected()->permissionDenied;
	$template->text = L::translate($e->getMessage())." ".
		L::selected()->incidentLogged.(DEBUG?' <pre>'."Stack Trace:\n".
		$e->getTraceAsString().'</pre>':'');
	$template->render('error');
}
catch(NotImplementedException $e)
{
	Logger::log($e->getMessage(), 'warning');
	$template = Template::instance();
	$template->title = L::selected()->notImplemented;
	$template->text = L::translate($e->getMessage()).
		(DEBUG?' <pre>'."Stack Trace:\n".$e->getTraceAsString().'</pre>':'');
	$template->render('error');
}
catch(Exception $e)
{
	Logger::log($e->getMessage(), 'error');
	$template = Template::instance();
	$template->title = L::selected()->error;
	$template->text = L::translate($e->getMessage()).
		(DEBUG?' <pre>'."Stack Trace:\n".$e->getTraceAsString().'</pre>':'');
	$template->render('error');
}
	
